﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.IO.IsolatedStorage;
using System.Windows.Navigation;
using System.Windows.Media.Imaging;
using System.Xml;
using System.Text.RegularExpressions;
using System.Xml.Linq;
using Common.Library;
using Common.Utilities;


namespace CommonControls
{
    public partial class Feedback : PhoneApplicationPage
    {
        #region Constructor

        public Feedback()
        {
            InitializeComponent();

            LoadFeedbackTopics();
            LayoutRoot.Background = ImageHelper.LoadPivotBackground();
            Loaded += new RoutedEventHandler(Feedback_Loaded);
        }

        #endregion

        private void LoadFeedbackTopics()
        {
            List<FeedbackTopic> objfeedback = new List<FeedbackTopic>();

            XDocument xdoc = new XDocument();
            xdoc = XDocument.Load(Constants.FeedbackTopicsXmlPath);

            var xquery = from i in xdoc.Descendants("topic") select i;
            foreach (var item in xquery)
            {
                FeedbackTopic objmenu = new FeedbackTopic();
                objmenu.Topic = item.Value;
                objfeedback.Add(objmenu);
            }

            TopicListPicker.ItemsSource = objfeedback;
        }

        #region Events

        void Feedback_Loaded(object sender, RoutedEventArgs e)
        {
            string LinkType = string.Empty;
            
            if(NavigationContext.QueryString.TryGetValue("LinkType", out LinkType))
            LayoutRoot.Background = ImageHelper.LoadPivotBackground();
            if (LinkType == "Songs" || LinkType=="Movies")
            {
                txtboxFeedback.Text = PageHelper.GetFeedbackMailMessage(NavigationContext);
            }
            else
            {
                txtboxFeedback.Text = PageHelper.GetFeedbackMailMessageForAudio(NavigationContext);
            }
           
            tbxMailId.Text = AppSettings.FeedbackEmailID;
        }

        private void ApplicationBarIconSendEmailButton_Click(object sender, EventArgs e)
        {
            try
            {
                if (TopicListPicker.SelectedIndex != -1)
                {
                    if (txtboxFeedback.Text != "")
                    {
                        tblkMessageValidation.Visibility = Visibility.Collapsed;
                        string body = txtboxFeedback.Text + ResourceHelper.GetMailMessageInfo();
                        string fromAddress = tbxMailId.Text;
                        if (fromAddress != "")
                        {
                            if (ValidationHelper.IsEmailAdressValid(fromAddress))
                            {
                                tblkMailIdValidation.Text = "(optional)";
                                tblkMailIdValidation.Foreground = AppResources.WhiteBrush;
                              fromAddress = CloudService.SendMail((TopicListPicker.SelectedItem as FeedbackTopic).Topic, body, fromAddress);
                            }
                            else
                            {
                                tblkMailIdValidation.Text = "Please Enter Correct Mail ID";
                                tblkMailIdValidation.Foreground = AppResources.RedBrush;
                            }
                        }
                        else
                        {
                            fromAddress = AppResources.FromMailID;
                            fromAddress = CloudService.SendMail((TopicListPicker.SelectedItem as FeedbackTopic).Topic, body, fromAddress);
                        }
                    }



                    else
                        tblkMessageValidation.Visibility = Visibility.Visible;
                }
            }
            catch (Exception ex)
            {
                Exceptions.SaveOrSendExceptions("Exception in ApplicationBarIconButton_Click Method In Feedback file.", ex);
            }
        }

        #endregion
    }
}